package com.gblw.rjkf.pj.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.gblw.rjkf.pj.entity.SysUser;
import com.gblw.rjkf.pj.service.SysUserService;
import com.gblw.rjkf.pj.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (SysUser)表控制层
 *
 * @author makejava
 * @since 2024-08-13 15:24:41
 */
@RestController
@RequestMapping("sys/sysUser")
public class SysUserController {
    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;

    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     */
    @SaCheckPermission(value = "sys:user:list")
    @GetMapping("/list")
    public Result selectAll(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                            @RequestParam(value = "userName") String userName,
                            @RequestParam(value = "nickName") String nickName) {
        return sysUserService.page(pageNum, pageSize, userName, nickName);
    }

    /**
     * 通过主键查询单条数据
     *
     * @return 单条数据
     */
    @SaCheckPermission(value = "sys:user:list")
    @GetMapping("/getById")
    public Result selectOne() {
        return Result.success("查询成功", sysUserService.getById((Serializable) StpUtil.getLoginId()));
    }

    /**
     * 新增数据
     *
     * @param sysUser 实体对象
     * @return 新增结果
     */
    @SaCheckPermission(value = "sys:user:insert")
    @PostMapping("/insert")
    public Result insert(@RequestBody SysUser sysUser) {
        return Result.success("查询成功", sysUserService.save(sysUser));
    }

    /**
     * 修改数据
     *
     * @param sysUser 实体对象
     * @return 修改结果
     */
    @SaCheckPermission(value = "sys:user:update")
    @PostMapping("/update")
    public Result update(@RequestBody SysUser sysUser) {
        return sysUserService.updateUser(sysUser);
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @SaCheckPermission(value = "sys:user:delete")
    @DeleteMapping("/deleteIds")
    public Result delete(@RequestParam("idList") List<Serializable> idList) {
        return Result.success("删除", sysUserService.removeByIds(idList));
    }

    /**
     * 批量删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @SaCheckPermission(value = "sys:user:delete")
    @ApiOperation("单个删除数据")
    @PostMapping("/deleteIds/{id}")
    public Result delete(@PathVariable Integer id) {
        return sysUserService.deleteUser(id);
    }

    @SaCheckPermission(value = "sys:user:insert")
    @PostMapping("/insertUser")
    public Result insertUser(@RequestBody SysUser sysUser) {
        return sysUserService.insertUser(sysUser);
    }
}

